package com.funo.wcity.app.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.funo.wcity.app.entity.TblContactPersons;

@Repository("tblContactPersonsDAO")
public class TblContactPersonsDAO extends DefaultBaseDao<TblContactPersons,Integer>{

	/**
	 * 通过员工id，查询所关联的联系人
	 * @param
	 * 		idEmployees
	 * 			员工id
	 * @param
	 * 		idCompanyType
	 * 			公司类型
	 */
	public List getContactPersonslList(String idEmployees,Integer idCompanyType) {
		StringBuffer sql = new StringBuffer(
				"select p.* from tblcompanytypes type,tblcompanies c,tblcontactpersons p " +
				"where type.idCompanyType=c.idCompanyType and c.idCompany=p.idCompany and type.idCompanyType="+idCompanyType+
				" and c.idEmployee in("+idEmployees+") order by c.lastDate,p.lastDate");
		return this.jdbcTemplate.queryForList(sql.toString());
	}
	
	/**
	 * 通过员工id，查询所关联的联系人
	 */
	public List getContactPersonslListByHibernate(String idEmployees,Integer idCompany) {
		DetachedCriteria dcriteria = DetachedCriteria.forClass(TblContactPersons.class);
		dcriteria.createAlias("tblcompanies", "companies");
		if(StringUtils.isNotBlank(idEmployees)){
			String[] s = idEmployees.split(",");
			List<Integer> idNums = new ArrayList<Integer>();
			for(String i : s){
				idNums.add(Integer.parseInt(i));
			}
			dcriteria.add(Restrictions.in("companies.idEmployee", idNums));
		}
		if(idCompany!=null){
			dcriteria.add(Restrictions.eq("companies.idCompany", idCompany));
		}
		return this.hibernateTemplate.findByCriteria(dcriteria);
	}
	
	/**
	 * 通过查询字符串模糊查询所关联的联系人
	 */
	public List getContactPersonslListByLike(String idEmployees,String queryStr) {
		DetachedCriteria dcriteria = DetachedCriteria.forClass(TblContactPersons.class);
		dcriteria.createAlias("tblcompanies", "companies");
		if(StringUtils.isNotBlank(idEmployees)){
			String[] s = idEmployees.split(",");
			List<Integer> idNums = new ArrayList<Integer>();
			for(String i : s){
				idNums.add(Integer.parseInt(i));
			}
			dcriteria.add(Restrictions.in("companies.idEmployee", idNums));
		}
		if(StringUtils.isNotBlank(queryStr)){
			dcriteria.add(Restrictions.or(Restrictions.sqlRestriction("concat(firstName,lastName) like '%"+queryStr+"%'"),Restrictions.like("email", queryStr,MatchMode.ANYWHERE)));
		}
		return this.hibernateTemplate.findByCriteria(dcriteria);
	}

	/**
	 * 通过联系人帐号，得出对应的联系人
	 * @param
	 * 		email
	 * 			邮箱
	 */
	public TblContactPersons getTblContactPersons(String email){
		StringBuffer hql=new StringBuffer("From TblContactPersons t where 1=1 and t.email='").append(email).append("'");
		List<TblContactPersons> contactPersonsList =this.hibernateTemplate.find(hql.toString());
		if(contactPersonsList.size()>0){
			return contactPersonsList.get(0);
		}else{
			return null;
		}
	}
	
	public void deleteTblContactPersons(String idContactPersons){
		if(StringUtils.isNotBlank(idContactPersons)){
			String hql="delete from TblContactPersons t where t.idContactPerson in ("+idContactPersons.substring(0, idContactPersons.length()-1)+")";
			this.hibernateTemplate.bulkUpdate(hql);
		}
	}
}
